<?php

namespace App\Http\Controllers\Merchants;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FormController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        
            }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */


    //添加商品
    public function store(Request $request)
    {
        //

            $file = $request->file('photo');
           if(!$file){

                $url = 'images/photo/default.jpg';
             $id=$request->id;
          $arr = $request->all();
          $arr = array_except($arr, ['_token']);
           $arr = array_except($arr, ['photo']);
            $arr = array_add($arr, 'photo',$url);

       
          $new= DB::table('goods')
          
            ->insert($arr);
            return back();
           }


          $id=$request->id;

            // dd($id);
             //确认上传的文件是否成功
            if($file->isValid()){

                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/photo/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
                $url = $photo_path.$filename;

          

            

              $id=$request->id;
          $arr = $request->all();
          $arr = array_except($arr, ['_token']);
           $arr = array_except($arr, ['photo']);
            $arr = array_add($arr, 'photo',$url);

       
          $new= DB::table('goods')
          
            ->insert($arr);
            return back();

              
         

            }else{

               return back();

            }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //表单显示

        $cats = DB::table('cats')->get();

         $data = DB::table("goods")->where('mid',$id)->get();
        $col = DB::table('foodbar')->get();
        return view('merchant.shopadd')->with('data',$data)->with('cats',$cats)->with('col',$col);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //


    }


    //图片上传
    public function photo(Request $request)
    {

     
            $file = $request->file('goods');
      
            $id=$request->id;
             //确认上传的文件是否成功
            if($file->isValid()){

                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/photo/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
                $url = $photo_path.$filename;

               dd($url);

                 $new= DB::table('merchant')
            ->where('id', $id)
            ->update(['photo'=>$url]);
              
            return back();

            }else{

               return back();

            }
        

    }
}
